package com.example.administrator.u17.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;

import com.example.administrator.u17.R;
import com.example.administrator.u17.fragment.MineFragment;

import java.io.File;

public class MineTitlePicActivity extends AppCompatActivity implements View.OnClickListener{

    public static final int REQUEST_CODE = 1616;
    private ImageView ivMinePic,btnOpen,ivBack;
    private String mPath;
    private FragmentTransaction transaction;
    private FragmentManager fragmentManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_mine_title_pic);
        initView();
    }

    private void initView() {
        ivMinePic= (ImageView) findViewById(R.id.iv_mineTitlePic_1);
        btnOpen= (ImageView) findViewById(R.id.btn_mine_title_pic);
        ivBack= (ImageView) findViewById(R.id.iv_mineTitlePic);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//               Bundle bundle=new Bundle();
//                bundle.getString("mPath");
//               fragmentManager=getSupportFragmentManager();
//                transaction=fragmentManager.beginTransaction();
//                MineFragment mineFragment=new MineFragment();
//                mineFragment.setArguments(bundle);
//                transaction.commit();
                Intent intent=new Intent(MineTitlePicActivity.this,MainActivity.class);
                intent.putExtra("mPath",mPath);
                setResult(200,intent);
                finish();
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_mine_title_pic:
                //判断当前是否有这个权限
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
                        == PackageManager.PERMISSION_GRANTED){
                    Intent intent=new Intent();
                    intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(intent,REQUEST_CODE);
                }else {
                    ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA},1616);
                }
            break;
            case R.id.bt_main_file:
                if (ContextCompat.checkSelfPermission(this,Manifest.permission
                .CAMERA)==PackageManager.PERMISSION_GRANTED){
                    Intent intent=new Intent();
                    intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                    mPath= Environment.getExternalStorageDirectory()+ File.separator+
                            "1616.jpg";
                    intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(mPath)));
                    startActivityForResult(intent,16);
                }else {
                    ActivityCompat.requestPermissions(this,new String[]{
                            Manifest.permission.CAMERA},16);

                }
                break;
        }


    }

    //请求权限的结果
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        // 判断是否是我们发出去的权限请求  requestCode == 1616
        // 判断有没有获取到权限  ntResults[0]==PackageManager.PERMISSION_GRANTED
        if (requestCode == 1616&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
            Intent intent = new Intent();
            intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent,REQUEST_CODE);
        }

        if (requestCode==16&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
            Intent intent = new Intent();
            intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
            mPath = Environment.getExternalStorageDirectory()+ File.separator+"1616.jpg";
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(mPath)));
            startActivityForResult(intent,16);
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE&&resultCode== Activity.RESULT_OK&&data!=null){
            Bundle bundle = data.getExtras();
            Bitmap bitmap = (Bitmap) bundle.get("data");
            if (bitmap!=null){
                ivMinePic.setImageBitmap(bitmap);
            }
        }
        if (requestCode == 16&&resultCode == Activity.RESULT_OK){

            BitmapFactory.Options options = new BitmapFactory.Options();
            // 只加载bitmap的宽高信息 不是真正的bitmap 而是一个空的 所以不会占用很大的内容
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(mPath, options);
            // 获取bitmap的宽高信息
            int outHeight = options.outHeight;
            int outWidth = options.outWidth;
            //算出压缩比
            int sample = Math.max(outHeight/200,outWidth/200);
            //设置压缩比
            options.inSampleSize = sample;
            //开始加载真正bitmap 并压缩
            options.inJustDecodeBounds = false;
            Bitmap bitmap = BitmapFactory.decodeFile(mPath, options);
            if (bitmap!=null){
                ivMinePic.setImageBitmap(bitmap);
            }
        }
    }
}
